package com.olap.starter.core.response.paging;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.olap.starter.util.servlet.ServletUtils;

/**
 * @author WBG
 * @date 2021/9/6 7:59
 * @describe
 */
public class PageRequest {
    /**
     * 当前记录起始索引
     */
    public static final String PAGE_NUMBER = "pageIndex";

    /**
     * 每页显示记录数
     */
    public static final String PAGE_SIZE = "pageSize";

    /**
     * 排序列
     */
    public static final String ORDER_BY_COLUMN = "orderByColumn";

    /**
     * 排序的方向 "desc" 或者 "asc".
     */
    public static final String IS_ASC = "isAsc";

    /**
     * 封装分页对象
     */
    public static BasePage doGetBasePage() {
        BasePage basePage = new BasePage();
        Integer pageNumber = ServletUtils.getParameterToInt(PAGE_NUMBER);
        if (pageNumber != null) {
            basePage.setPageNum(pageNumber);
        }
        Integer pageSize = ServletUtils.getParameterToInt(PAGE_SIZE);
        if (pageSize != null) {
            basePage.setPageSize(pageSize);
        }
        basePage.setOrderByColumn(ServletUtils.getParameter(ORDER_BY_COLUMN));
        basePage.setIsAsc(ServletUtils.getParameter(IS_ASC));
        return basePage;
    }

    public static IPage doIPage() {

        Integer pageNumber = ServletUtils.getParameterToInt(PAGE_NUMBER);
        Integer pageSize = ServletUtils.getParameterToInt(PAGE_SIZE);
        if (pageSize == null) {
            pageSize = 20;
        }
        if (pageNumber == null) {
            return new Page();
        }
        return new Page(pageNumber, pageSize);
    }

    public static BasePage getBasePage() {
        return doGetBasePage();
    }
}
